package exttable.testversioning.testversioncreate;

import exttable.testversioning.tablemodel.AnalysisVersionModel;
import utils.Localisation;

import javax.swing.*;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Jun 18, 2010
 * Time: 2:20:06 PM
 * To change this template use File | Settings | File Templates.
 */
public class CreateAVView {
    private SpringLayout layout = new SpringLayout();
    private JFrame frame;
    private JLabel versionLabel;
    private JLabel version;
    private JScrollPane tableScrollFill;
    private JButton backButton;
    private JButton nextButton;
    private JButton up;
    private JButton down;
    private JTable unitTable;
    private JButton saveData;
    private JComboBox unitsList;
    private CreateAVModel model;

    public CreateAVView(CreateAVModel model) {
        this.model = model;
        init();
    }

    public void init() {

        try {
            frame = new JFrame();
            frame.setLayout(layout);
            frame.setTitle(model.getViewAnalysis().toString());
            unitsList = new JComboBox(model.getVersionModel().getUnits());
            backButton = new JButton(Localisation.getMessage("form.back"));
            nextButton = new JButton(Localisation.getMessage("form.continue"));
            saveData = new JButton(Localisation.getMessage("error.save"));
            saveData.setVisible(false);
            up = new JButton("UP");
            down = new JButton("DOWN");
            versionLabel = new JLabel(Localisation.getMessage("form.version"));
            version = new JLabel(model.getVersionAnalysis().toString());
            unitTable = new JTable(new AnalysisVersionModel(model.getOriginalTableData(), true));
            unitTable.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(new JComboBox(model.getVersionModel().getUnits())));

            tableScrollFill = new JScrollPane(unitTable);
            frame.add(tableScrollFill);
            frame.add(versionLabel);
            frame.add(version);
            frame.add(nextButton);
            frame.add(backButton);
            frame.add(saveData);
            frame.add(up);
            frame.add(down);
            organiseLayout();
            frame.setSize(600, 600);
            frame.setLocation(200, 100);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void organiseLayout() {
        layout.putConstraint(SpringLayout.WEST, versionLabel, 15, SpringLayout.WEST, frame);
        layout.putConstraint(SpringLayout.NORTH, versionLabel, 15, SpringLayout.SOUTH, frame);

        layout.putConstraint(SpringLayout.WEST, version, 15, SpringLayout.EAST, versionLabel);
        layout.putConstraint(SpringLayout.NORTH, version, 15, SpringLayout.SOUTH, frame);

        layout.putConstraint(SpringLayout.WEST, tableScrollFill, 15, SpringLayout.WEST, frame);
        layout.putConstraint(SpringLayout.NORTH, tableScrollFill, 25, SpringLayout.SOUTH, versionLabel);

        layout.putConstraint(SpringLayout.WEST, up, 15, SpringLayout.EAST, tableScrollFill);
        layout.putConstraint(SpringLayout.NORTH, up, 0, SpringLayout.NORTH, tableScrollFill);

        layout.putConstraint(SpringLayout.WEST, down, 15, SpringLayout.EAST, tableScrollFill);
        layout.putConstraint(SpringLayout.NORTH, down, 15, SpringLayout.SOUTH, up);

        layout.putConstraint(SpringLayout.WEST, backButton, 10, SpringLayout.WEST, frame);
        layout.putConstraint(SpringLayout.NORTH, backButton, 15, SpringLayout.SOUTH, tableScrollFill);

        layout.putConstraint(SpringLayout.WEST, nextButton, 90, SpringLayout.WEST, frame);
        layout.putConstraint(SpringLayout.NORTH, nextButton, 15, SpringLayout.SOUTH, tableScrollFill);

        layout.putConstraint(SpringLayout.WEST, saveData, 15, SpringLayout.EAST, nextButton);
        layout.putConstraint(SpringLayout.NORTH, saveData, 15, SpringLayout.SOUTH, tableScrollFill);
    }

    void makeVisible() {
        frame.setVisible(true);
    }

    public JTable getUnitTable() {
        return unitTable;
    }

    void addMultiplyListener(ActionListener next) {
        nextButton.addActionListener(next);
    }

    void addClearListener(ActionListener back) {
        backButton.addActionListener(back);
    }

    void addSaveAction(ActionListener save) {
        saveData.addActionListener(save);
    }

    void addUpAction(ActionListener upAction) {
        up.addActionListener(upAction);
    }

    void addDownAction(ActionListener downAction){
        down.addActionListener(downAction);
    }

    public JButton getBackButton() {
        return backButton;
    }

    public JButton getSaveData() {
        return saveData;
    }

    public JButton getNextButton() {
        return nextButton;
    }

    public JFrame getFrame() {
        return frame;
    }

    public JComboBox getUnitsList() {
        return unitsList;
    }

    public JButton getUp() {
        return up;
    }

    public JButton getDown() {
        return down;
    }
}



